package ex.recruit.action;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.Town;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.StandardGradientPaintTransformer;

import com.guinea.core.CustomBarRenderer;
import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.utils.ServletUtils;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAction;

import ex.recruit.service.ApplyBaseServiceEx;
import ex.recruit.service.TownServiceEx;
import ex.recruit.vo.ApplyJudgmentListVoEx;
import ex.recruit.vo.ApplyRegionListVoEx;

public class ApplyPlanReportAction extends StrutsAction {

	protected ApplyBaseServiceEx applyBaseServiceEx;
	TownServiceEx townServiceEx;

	public ApplyBaseServiceEx getApplyBaseServiceEx() {
		return applyBaseServiceEx;
	}

	public void setApplyBaseServiceEx(ApplyBaseServiceEx applyBaseServiceEx) {
		this.applyBaseServiceEx = applyBaseServiceEx;
	}

	public TownServiceEx getTownServiceEx() {
		return townServiceEx;
	}

	public void setTownServiceEx(TownServiceEx townServiceEx) {
		this.townServiceEx = townServiceEx;
	}

	/**
	 * url参数未定义method时的默认Action函数. 默认为index Action.
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return list(mapping, form, request, response);
	}

	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		List towns = getTownServiceEx().get();
		List result = new ArrayList();
		for (Iterator iterator = towns.iterator(); iterator.hasNext();) {
			Town town = (Town) iterator.next();
			ApplyRegionListVoEx applyRegionListVoEx = new ApplyRegionListVoEx();
			applyRegionListVoEx.setProvinceId(town.getProvinceId());
			applyRegionListVoEx.setCityId(town.getCityId());
			applyRegionListVoEx.setDistrictId(town.getDistrictId());
			applyRegionListVoEx.setConfirmAmount(0);
			applyRegionListVoEx.setFirstTrialAmount(0);
			applyRegionListVoEx.setVisitAmount(0);
			applyRegionListVoEx.setMatReviewAmount(0);
			applyRegionListVoEx.setSknAuditAmount(0);
			applyRegionListVoEx.setFinalJudgmentAmount(0);
			
			result.add(applyRegionListVoEx);
		}
		
		//匹配到区
		List applys = getApplyBaseServiceEx().getApplySubmitted();
		List applysLeft = new ArrayList();
		for (Iterator iterator = applys.iterator(); iterator.hasNext();) {
			ApplyRegionListVoEx a = (ApplyRegionListVoEx) iterator.next();
			boolean match = false;
			for (Iterator iterator2 = result.iterator(); iterator2.hasNext();) {
				ApplyRegionListVoEx t = (ApplyRegionListVoEx) iterator2.next();
				if(t.getProvinceId() != null && t.getProvinceId().equals(a.getProvinceId())
						&&t.getCityId() != null && t.getCityId().equals(a.getCityId())
						&&((t.getDistrictId() == null && a.getDistrictId() == null)
								||(t.getDistrictId() != null && t.getDistrictId().equals(a.getDistrictId())))){
					t.setConfirmAmount(a.getConfirmAmount() + t.getConfirmAmount());
					t.setFirstTrialAmount(a.getFirstTrialAmount() + t.getFirstTrialAmount());
					t.setVisitAmount(a.getVisitAmount() + t.getVisitAmount());
					t.setMatReviewAmount(a.getMatReviewAmount() + t.getMatReviewAmount());
					t.setSknAuditAmount(a.getSknAuditAmount() + t.getSknAuditAmount());
					t.setFinalJudgmentAmount(a.getFinalJudgmentAmount() + t.getFinalJudgmentAmount());
					//匹配到，移除
					match = true;
					break;
				}
			}
			if(!match){
				applysLeft.add(a);
			}
		}
		//匹配到市
		for (Iterator iterator = applysLeft.iterator(); iterator.hasNext();) {
			ApplyRegionListVoEx a = (ApplyRegionListVoEx) iterator.next();
			for (Iterator iterator2 = result.iterator(); iterator2.hasNext();) {
				ApplyRegionListVoEx t = (ApplyRegionListVoEx) iterator2.next();
				if(t.getProvinceId() != null && t.getProvinceId().equals(a.getProvinceId())
						&&t.getCityId() != null && t.getCityId().equals(a.getCityId())){
					t.setConfirmAmount(a.getConfirmAmount() + t.getConfirmAmount());
					t.setFirstTrialAmount(a.getFirstTrialAmount() + t.getFirstTrialAmount());
					t.setVisitAmount(a.getVisitAmount() + t.getVisitAmount());
					t.setMatReviewAmount(a.getMatReviewAmount() + t.getMatReviewAmount());
					t.setSknAuditAmount(a.getSknAuditAmount() + t.getSknAuditAmount());
					t.setFinalJudgmentAmount(a.getFinalJudgmentAmount() + t.getFinalJudgmentAmount());
					
					break;
				}
			}
		}
		
		request.setAttribute("result", result);

		return mapping.findForward("list");
	}

}
